<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>设置角色</title>
    <!-- 控制页面其他url路径的前缀 -->
    <base href="${pageContext.request.contextPath}/">

    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="statics/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="statics/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
            <input type="hidden" name="userId" id="userId" value="${userId}">
            <c:forEach items="${roleList}" var="role" varStatus="sta">
                <input type="checkbox" name="role"
                        title="${role.roleName}"
                        ${role.isHave gt 0?'checked':''}
                        ${role.roleStatus ==0?'disabled':''}
                        value="${role.roleId}"
                        />
            </c:forEach>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="statics/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let userId = $("#userId").val();
            let checks = $("input[type=checkbox]:checked"); //获取选中的checkbox

            let roleIds = [];
            $.each(checks,function (i){
                roleIds.push(checks[i].value);
            });

            $.ajax({
                type:"post",
                url:"user/doSetRole",
                data: {"userId":userId,"roleIds":roleIds},
                dataType:"json",
                success:function(json){
                    if(json.code==200){
                        layer.msg(json.message, {icon: 1, shade: this.shade, scrollbar: false, time: 3000, shadeClose: true});
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        //刷新父页面中的数据表格
                        parent.layui.table.reload('currentTableId',{page: {curr: 1}});
                    }else if(json.code==300){
                        layer.msg(json.message, {icon: 2, shade: this.shade, scrollbar: false, time: 3000, shadeClose: true});
                    }
                }
            });
            return false;
        });

    });
</script>
</body>
</html>
